//
//  HJAddFriendController.m
//  KaiChat
//
//  Created by lx on 2023/10/21.
//

#import "HJAddFriendController.h"
#import "QMUITextField.h"
#import "TJEmptyDataHandler.h"
#import "HJAddFriendCell.h"
#import "HJIM.h"

@interface HJAddFriendController ()<QMUITextFieldDelegate>

@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, weak) QMUITextField *textField;
@property (nonatomic, assign) NSInteger lastSearchCount;


@end

@implementation HJAddFriendController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.title = @"Add Friends";
    self.navigationBar.lineView.hidden = YES;
    self.lastSearchCount = -1;

    UIView *headerView = [UIView addToView:self.view];
    headerView.backgroundColor = UIColor.whiteColor;
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(KNavBarHight);
        make.height.offset(60);
    }];
    
    UIView *searchView = [UIView addToView:headerView];
    searchView.backgroundColor = UIColor.tj_F6F6F6Color;
    searchView.addCornerRadius(10);
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.offset(10);
        make.height.offset(40);
    }];
    
    UIImageView *searchIcon = [UIImageView addToView:searchView];
    searchIcon.image = [UIImage imageNamed:@"search_icon"];
    [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(15);
        make.width.height.offset(20);
    }];
 
    QMUITextField *textField = [QMUITextField addToView:searchView];
    textField.placeholder = @"Enter user UID to search";
    textField.maximumTextLength = 20;
    textField.font = UIFontMake(14);
    textField.returnKeyType = UIReturnKeySearch;
    textField.delegate = self;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.equalTo(searchIcon.mas_right).offset(10);
        make.right.offset(0);
        make.height.offset(40);
    }];
    
    _emptyDataHandler =  [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView configuration:^TJEmptyDataType(TJEmptyDataModel * _Nonnull dataModel) {
        dataModel.iconName = @"search_empty";
        dataModel.text = @"results not found";
        return TJEmptyDataTypeBase;
    }];
    _textField = textField;
    
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
    [self.tableView registerClass:HJAddFriendCell.class forCellReuseIdentifier:@"HJAddFriendCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = UIColor.whiteColor;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJAddFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJAddFriendCell" forIndexPath:indexPath];
    HJChatUserModel *model = self.datas[indexPath.row];
    cell.model = model;
    TJWeakify(self);
    [cell setDidClickAddButtonBlock:^{
        [HJIM.shareIM.userManager sendApplyAddFriendWithUserId:model.userId complete:^(NSError * _Nullable error) {
            [weakself didClickSearchButton];
        }];
    }];
    
    return cell;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    [self didClickSearchButton];
    return YES;
}

- (void)didClickSearchButton {
    if (isEmptyString(_textField.text)) {
        [HJHUD showErrorMessage:_textField.placeholder];
        return;
    }
    [HJHUD showLoadingInView:self.view];
    [HJIM.shareIM.userManager searchFridendWithUid:_textField.text complete:^(HJChatUserModel * _Nullable model) {
        if (model) {
            self.datas = @[model];
        } else {
            self.datas = nil;
        }
        
        if (self.datas.count == 0 && self.lastSearchCount == 0) return;

        self.emptyDataHandler.displayEmpty = YES;
        [self.tableView reloadData];
        self.lastSearchCount = self.datas.count;
    }];
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationFullScreen;
}

 
@end
